/*
 * Copyright 2009 André Luiz Alves Moraes
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * */
package org.alamoraes.cmdutils.parser;


import java.io.PrintStream;
import java.util.Collections;
import java.util.Comparator;
import java.util.Hashtable;
import java.util.List;
import java.util.Vector;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author andre
 */
public class AmCmdLineParser {
    private Hashtable<String, CommandLineParameter> params;

    public AmCmdLineParser() {
        params = new Hashtable<String, CommandLineParameter>();
    }
    
    public void parse(String[] argsArray) {
        ArrayWrapper<String> args = new ArrayWrapper<String>(argsArray);
        while(!args.isEof()) {
            String paramName = args.moveNext();
            if (paramName.startsWith("--")) {
                paramName = paramName.substring(2);
                if (!params.containsKey(paramName)) {
                    continue;
                } else {
                    CommandLineParameter clp = params.get(paramName);
                    if (clp.isHasValue()) {
                        String value = args.moveNext();
                        clp.setValue(value);
                    } else {
                        clp.markAsSet();
                    }
                }
            }
        }
    }

    public boolean isSet(String arg) {
        return params.get(arg).isSet();
    }

    public String getValueOf(String arg) {
        return params.get(arg).getValue();
    }

    public void addSwitch(String name, String description, boolean required) {
        CommandLineParameter clp = new CommandLineParameter(name, required, description, null, false, false);

        params.put(clp.getParamName(), clp);
    }

    public void addParam(String name, String description, boolean required) {
        CommandLineParameter clp = new CommandLineParameter(name, required, description, null, required, true);
        
        params.put(clp.getParamName(), clp);
    }

    public boolean validate() {
        for(CommandLineParameter clp: params.values()) {
            if (clp.isRequired() && !clp.isSet()) {
                return false;
            } else {
                if (clp.isHasValue() && !clp.isSet()) {
                    return false;
                }
            }
        }
        return true;
    }

    protected String buildUnsetParameterMessage(CommandLineParameter clp) {
        return "Param: " + clp.getParamName() + " is required and is not set";
    }

    protected String buildUnsetParameterValueMessage(CommandLineParameter clp) {
        return "Param: " + clp.getParamName() + " requires a value";
    }

    public void printUsage(PrintStream pw ) {
        List<CommandLineParameter> paramsList = new Vector<CommandLineParameter>();
        for(CommandLineParameter clp: params.values())
            paramsList.add(clp);
        
        Collections.sort(paramsList, new Comparator<CommandLineParameter>() {

            public int compare(CommandLineParameter o1, CommandLineParameter o2) {
                return o1.getParamName().compareTo(o2.getParamName());
            }
        });

        for(CommandLineParameter clp: paramsList) {
            pw.println(clp);
            pw.println();
        }
    }
}
